package year2022.month456789;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamTest {

    public static void main(String[] args){
        List<Integer> list = new ArrayList<Integer>(){{
            add(1);
            add(2);
            add(3);
            add(4);
            add(5);
            add(6);
        }};
        list.forEach(System.out::println);

        System.out.println("====================");

        List<Integer> halfList = StreamTest.pull(list, o->{
            if (o%2==0) {
                return o;
            }
            return null;
        });
//        halfList.remove(null);
        halfList.forEach(System.out::println);

        System.out.println("====================");

        halfList.addAll(new ArrayList<>());
        halfList.forEach(System.out::println);

/*        List<People> peopleList = new ArrayList<People>(){{
            add(new People("1", null));
            add(new People("2", null));
            add(new People("3", null));
            add(new People("4", null));
            add(new People("5", null));
            add(new People("6", null));
        }};
        peopleList.forEach(System.out::println);

        halfList = StreamTest.pull(peopleList, People::getAge);
//        halfList.remove(null);
        halfList.forEach(System.out::println);*/
    }

    public static <R, T> List<R> pull(List<T> list, Function<T, R> function){
        if (list==null || list.size()==0 || function==null){
            return new ArrayList<R>(); //方便使用addAll
        }
        Set<R> result = new HashSet<R>(list.stream().map(function).distinct().collect(Collectors.toList()));
        result.remove(null);
        return result.stream().distinct().collect(Collectors.toList());
    }

}

class People {
    private String name;
    private Integer age;

//    public People(){}

    public People(String name, Integer age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

class Man extends People {

    public Man() {
        super("无名", 10);
    }

}